home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------------------
- #
- # TV-Man.Utility.c
- #
- # Copyright © Apple Computer, Inc. 1989-1990
- # All rights reserved.
- #
- #
- #
- # This is a collection of functions that are not specificaly dedicated to
- # any one part of the application but can be used across the board. See the following
- # paragraphs for more definition of what can be included in this file.
- #
- # In order to have an evironment which predictable events happen several definitions
- # must be established. The most critical is in the file architecture. Each functional
- # block will have its own source and header file. The main project file, in this
- # case TV-Man, will have its header file included with all other files. This will
- # allow for global constants. The utility source file shall contain functions that
- # are general purpose in nature and that can be used by all other functions. These
- # are intended not to be application or major block specific. In order for this to be
- # accomodated all functions in the utility source file must use only the information
- # that is passed to them or information that can be gleaned from the system via
- # toolbox calls. There will be no header file associated with the utility file as this
- # will destroy the intent of the utilities.
- #
- # There are several files which contain information which is global in nature .These
- # file are included in the main project header file. They are: x.Errors.h, x.Ext.h,
- # x.Protos.h, x.Menus.h. The reason for containing them in seperate files is one of
- # convienience and accesability.
- #
- #
- # Revision Log:
- #
- # 9-4-91 RGK Creation
- #
- #
- ------------------------------------------------------------------------------*/
-
- /* This include is here for the inclusion of the standard toolbox and system
- definitions. This is not to be confused with the other definitions which
- become avaliable with this file. That would null and void the standard disclamer
- in the header statements */
-
- #include "TV-Man.h"
-
-
- /*---------------------------------------------------------------------------------------*/
- /* This is called when an update event is received for a window.
- It calls DrawWindow to draw the contents of an application window.
- As an effeciency measure that does not have to be followed, it
- calls the drawing routine only if the visRgn is non-empty. This
- will handle situations where calculations for drawing or drawing
- itself is very time-consuming. */
-
- void DoUpdate(window)
- WindowPtr window;
- {
- if ( IsAppWindow(window) )
- {
- BeginUpdate(window);
- if ( ! EmptyRgn(window->visRgn) ) /* draw if updating needs to be done */
- DrawWindow(window);
- EndUpdate(window);
- }
- } /* DoUpdate */
-
-
-
-
-
-
- /*---------------------------------------------------------------------------------------*/
- /* This is called when a window is activated or deactivated.
- In TV-Man, the Window Manager's handling of activate and
- deactivate events is sufficient */
-
- void DoActivate(window, becomingActive)
- WindowPtr window;
- Boolean becomingActive;
- {
- if ( IsAppWindow(window) )
- {
- if ( becomingActive )
- /* do whatever you need to at activation */ ;
- else
- /* do whatever you need to at deactivation */ ;
- }
- } /* DoActivate */
-
-
-
-
-
- /*---------------------------------------------------------------------------------------*/
- /* Get the global coordinates of the mouse. When you call OSEventAvail
- it will return either a pending event or a null event. In either case,
- the where field of the event record will contain the current position
- of the mouse in global coordinates and the modifiers field will reflect
- the current state of the modifiers. Another way to get the global
- coordinates is to call GetMouse and LocalToGlobal, but that requires
- being sure that thePort is set to a valid port. */
-
- void GetGlobalMouse(mouse)
- Point *mouse;
- {
- EventRecord event;
-
- OSEventAvail(kNoEvents, &event); /* we aren't interested in any events */
- *mouse = event.where; /* just the mouse position */
- } /* GetGlobalMouse */
-
-
-
-
-
- /*---------------------------------------------------------------------------------------*/
- /* Check to see if a window belongs to the application. If the window pointer
- passed was NIL, then it could not be an application window. WindowKinds
- that are negative belong to the system and windowKinds less than userKind
- are reserved by Apple except for windowKinds equal to dialogKind, which
- mean it is a dialog. In order to reduce the chance of accidentally treating
- some window as an AppWindow that shouldn't be, we'll only return true if the
- windowkind is userKind. If you add different kinds of windows to TV-Man
- you'll need to change how this all works. */
-
- Boolean IsAppWindow(window)
- WindowPtr window;
- {
- short windowKind;
-
- if ( window == nil )
- return false;
- else
- { /* application windows have windowKinds = userKind (8) */
- windowKind = ((WindowPeek) window)->windowKind;
- return (windowKind == userKind);
- }
- } /* IsAppWindow */
-
-
-
-
-
-
- /*---------------------------------------------------------------------------------------*/
- /* Check to see if a window belongs to a desk accessory. */
-
- Boolean IsDAWindow(window)
- WindowPtr window;
- {
- if ( window == nil )
- return false;
- else /* DA windows have negative windowKinds */
- return ((WindowPeek) window)->windowKind < 0;
- } /* IsDAWindow */
-
-
-
-
-
-
- /*---------------------------------------------------------------------------------------*/
- /* Display an alert that tells the user an error occurred. If the error type is "eMajor"
- then exit the program. This is used as an ultimate bail-out for serious errors that
- prohibit the continuation of the application. The error number is used to index an
- 'STR#' resource so that a relevant message can be displayed. */
-
- void Error(type, error)
- short type, error;
- {
- short itemHit;
- Str255 message;
-
- SetCursor(&qd.arrow);
- GetIndString(message, type, error);
- ParamText(message, "", "", "");
- itemHit = Alert(type, nil);
- if(type == eMajor) ExitToShell();
- } /* Error */
-
-
-
-
-
-
-
- /*---------------------------------------------------------------------------------------*/
- /* Close a window. This handles desk accessory and application windows. If there was
- a document associated with a window, you could do any document saving processing
- if it is 'dirty'.DoCloseWindow would return true if the window actually closed, i.e.,
- the user didn’t cancel from a save dialog. This result is handy when
- the user quits an application, but then cancels the save of a document
- associated with a window. */
-
-
-
- Boolean DoCloseWindow(window)
- WindowPtr window;
- {
- if ( IsDAWindow(window) )
- CloseDeskAcc(((WindowPeek) window)->windowKind);
- else if ( IsAppWindow(window) )
- CloseWindow(window);
- return true;
- } /* DoCloseWindow */
-
-
-
-
-
-
-
- /*---------------------------------------------------------------------------------------*/
- /* Clean up the application and exit. We close all of the windows so that
- they can update their documents, if any. If we find out that a cancel has
- occurred, we won't exit to the shell, but will return instead. */
-
- void Terminate()
- {
- WindowPtr aWindow;
- Boolean closed;
-
- closed = true;
- do
- {
- aWindow = FrontWindow(); /* get the current front window */
- if (aWindow != nil)
- closed = DoCloseWindow(aWindow); /* close this window */
- }
- while (closed && (aWindow != nil));
- if (closed)
- ExitToShell(); /* exit if no cancellation */
- } /* Terminate */
-
-
-
-
-
-
- /*----------------------------------------------------------------------------------*/
- /* This function checks the value of the edit box against the values passed. If the
- value in the edit box is over the limits passed the edit box will be modified
- to reflect these limits. */
-
- void RangeCheckShort(dialog, itemid, minimum, maximum)
- DialogPtr dialog;
- short itemid, minimum, maximum;
- {
- short textvalue, value;
-
- textvalue = Text2Short(dialog, itemid);
- value = CheckValueRange(textvalue, minimum, maximum);
- Short2Text(value, dialog, itemid);
- }/* RangeCheckShort */
-
-
-
-
-
-
- /*----------------------------------------------------------------------------------*/
- /* this function sets the specified text box id with the value passed */
-
- void Short2Text(value, dialog, itemid)
- short value, itemid;
- DialogPtr dialog;
- {
- short itemtype;
- Handle itemhdl;
- Rect itemrect;
- Str255 string;
-
- NumToString((long)value, &string);
- GetDItem(dialog, itemid, &itemtype, &itemhdl, &itemrect);
- SetIText(itemhdl, string);
- }/* SetTheText */
-
-
-
-
-
-
- /*----------------------------------------------------------------------------------*/
- /* This function gets the specified text box id with the value passed. Since the
- GetIText returns a long and we need to return a short a test is made on the value
- to determine how to truncate it. */
-
- short Text2Short(dialog, itemid)
- short itemid;
- DialogPtr dialog;
- {
- short itemtype;
- Handle itemhdl;
- Rect itemrect;
- long longvalue;
- Str255 string;
-
- GetDItem(dialog, itemid, &itemtype, &itemhdl, &itemrect);
- GetIText(itemhdl, &string);
- StringToNum(string, &longvalue);
- if(longvalue > 32767) return(32767);
- if(longvalue < -32768) return(-32768);
- return((short)LoWrd(longvalue));
- }/* GetTheText */
-
-
-
-
-
-
- /*----------------------------------------------------------------------------------*/
- /* This function toggles the control value from the Min control value to the Max
- control value. The control handle is passed to identify which control is to be
- toggled. */
-
- void ToggleControl(control)
- ControlHandle control;
- {
- if(GetCtlValue(control) == GetCtlMax(control))
- {
- SetCtlValue(control, GetCtlMin(control));
- }
- else
- {
- SetCtlValue(control, GetCtlMax(control));
- }
- }/* ToggleControl */
-
-
-
-
-
-
- /*----------------------------------------------------------------------------------*/
- /* This function returns a value which divides the range of the scroll area of the
- control passed with the amount passed. The actual number may be off because of
- roundoff errors. Because of this the calling function should make sure the control
- value dosn't extend beyond the controls limits. */
-
- short JumpAmount(control, amount)
- ControlHandle control;
- short amount;
- {
- short value;
-
- value = GetCtlMax(control) - GetCtlMin(control);
- value = value / amount;
- return(value);
- }/* JumpAmount */
-
-
-
-
-
-
-
-
- /*----------------------------------------------------------------------------------*/
- /* This function tests the passed value to see if it is in the range specified by
- the Minimum and Maximum levels in the control identified by the passed handle. If
- the value is in the range the value will be returned unchanged. If either limit
- is exceeded the returned value will be closest limit value. */
-
- short CheckControlRange(value, control)
- short value;
- ControlHandle control;
- {
- if(value > GetCtlMax(control)) return(GetCtlMax(control));
- if(value < GetCtlMin(control)) return(GetCtlMin(control));
- return(value);
- }/* CheckControlRange */
-
-
-
-
-
-
-
- /*----------------------------------------------------------------------------------*/
- /* This function tests the passed value to see if it is in the range specified by
- the levels which are passed as well. If the value is in the range the value
- will be returned unchanged. If either limit is exceeded the returned value will
- be closest limit value. Since these are short integers there is a further test
- which will divide up the negative area to test for values which are over 32K
- but should not be interpreted as negative. This last part is sort of a hack */
-
- short CheckValueRange(value, minimum, maximum)
- short value, minimum, maximum;
- {
- if(value > maximum) return(maximum);
- if(value < minimum)
- {
- if(value < -16384)
- {
- return(maximum);
- }
- else
- {
- return(minimum);
- }
- }
- return(value);
- }/* CheckValueRange */